var sw2PublicKey = "040E279B33CCEDCAB5AB368BEA4243C7A0AABD0571819855A4C022E554065DCE68A6704974E611C63AA98534864BD1426969A5E8380F879AADD8EF14B595D277D5";
var passwordPublicKey;
var passwordmode = "default";

$(function(){
  $.ajax({
    url: "/api/api-system/system/core/public/getLoginConfig",
    type: "GET",
    success: function(response) {
      console.log("请求成功",response);
      // 处理返回的数据
      let result = response.data;

      //密码加密模式
      passwordmode = result.passwordmode;
      passwordPublicKey = result.passwordPublicKey;
    },
    error: function(xhr, status, error) {
      console.log("请求失败");
      // 处理错误
    }
  });
  $(".btn-submit").click(function () {
    let formAlias = "#" + this.id + "-form";
    let value = $(formAlias + ' input[name="password"]').val();
    if (passwordPublicKey) {
      value = getJsEncryptPassword(value.trim());
    } else if ("md5" == passwordmode) {
      value = md5(value.trim())
    } else if ("sm2" == passwordmode) {
      value = sm2Encrypt(value.trim())
    }
    $(formAlias + ' input[name="password"]').val(value);
    $(formAlias).submit();
  });
})

function getJsEncryptPassword(password){
  //jsencrypt
  // 新建JSEncrypt对象
  let encryptor = new JSEncrypt();
  // 设置公钥
  //encryptor.setPublicKey(publicKey);
  encryptor.setPublicKey(passwordPublicKey);
  var jm_data = encryptor.encrypt(password);
  // 加密数据
  return jm_data;
}
function sm2Encrypt(password) {
  let pwd= sm2.doEncrypt(password, sw2PublicKey);
  return pwd.toString();
}